﻿using HARTCalibrationTool.SerialPortManager;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HARTCalibrationTool
{
    public class DebugLogger
    {
        private string _logDirectory = null;
        private string _logFileName = null;
        private string _logFilePath = null;

        public DebugLogger() 
        { 
            
        }

        public void SetLogDir(string logDir)
        {
            _logDirectory = logDir;

            if (!Directory.Exists(_logDirectory))
            {
                Directory.CreateDirectory(_logDirectory);
            }

            _logFileName = DateTime.Now.ToString("yyyyMMddHHmm") + "_debug.log";
            _logFilePath = Path.Combine(_logDirectory, _logFileName);
        }

        public string GetLogFileDir()
        {
            return _logDirectory;
        }

        public void Log(string message)
        {
            try
            {
                string logMessage = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - {message}";

                using (StreamWriter writer = new StreamWriter(_logFilePath, append: true, encoding: Encoding.UTF8))
                {
                    writer.WriteLine(logMessage);
                }
            }
            catch (Exception ex)
            {
                SerialPortsBase.PrintMessage("Error writing to log: " + ex.Message);
            }
        }
    }
}
